﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClass;

namespace QuerySelectionLabelingTool
{
    public partial class GoldenSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            //set "query text" as "query link"
            CoverageOperation coverageOper = new CoverageOperation();
            for (int i = 0; i < this.GridView1.Rows.Count; ++i)
            {
                int taskID = Convert.ToInt32(this.GridView1.Rows[i].Cells[0].Text);
                int newsID = Convert.ToInt32(this.GridView1.Rows[i].Cells[2].Text);
                string query = this.GridView1.Rows[i].Cells[5].Text;
                WebQueryControl queryControl = (WebQueryControl)Page.LoadControl("WebQueryControl.ascx");
                queryControl.QueryText = query;
                queryControl.QueryLink = coverageOper.EncodeUrl(query, newsID);
                int position = coverageOper.GetPosition(taskID, newsID, query);
                queryControl.StateText = CoverageOperation.GetPosition(position);
                //exchange query
                this.GridView1.Rows[i].Cells[5].Controls.Add(queryControl);
            }
            //merge row
            List<int> splitTask = new List<int>();
            for (int i = 0; i < this.GridView1.Rows.Count; ++i)
            {
                GridViewRow r = this.GridView1.Rows[i];
                splitTask.Add(i);
                int rowSpan = 1;
                int j = 0;
                for (j = i + 1; j < this.GridView1.Rows.Count; ++j)
                {
                    GridViewRow rNext = this.GridView1.Rows[j];
                    if (r.Cells[0].Text == rNext.Cells[0].Text)
                    {
                        rNext.Cells[0].Visible = false;
                        rNext.Cells[1].Visible = false;
                        ++rowSpan;
                    }
                    else
                    {
                        i = j - 1;
                        break;
                    }
                }
                r.Cells[0].RowSpan = rowSpan;
                r.Cells[1].RowSpan = rowSpan;
                if (j == this.GridView1.Rows.Count)
                {
                    break;
                }
            }
            for (int i = 0; i < this.GridView1.Rows.Count; ++i)
            {
                GridViewRow r = this.GridView1.Rows[i];
                int rowSpan = 1;
                for (int j = i + 1; j < this.GridView1.Rows.Count; ++j)
                {
                    if (splitTask.FindIndex(delegate(int ii)
                    {
                        if (ii == j)
                        {
                            return true;
                        }
                        return false;
                    }) != -1)
                    {
                        i = j - 1;
                        break;
                    }
                    GridViewRow rNext = this.GridView1.Rows[j];
                    if (r.Cells[2].Text == rNext.Cells[2].Text)
                    {
                        rNext.Cells[2].Visible = false;
                        rNext.Cells[3].Visible = false;
                        rNext.Cells[4].Visible = false;
                        ++rowSpan;
                    }
                    else
                    {
                        i = j - 1;
                        break;
                    }
                }
                r.Cells[2].RowSpan = rowSpan;
                r.Cells[3].RowSpan = rowSpan;
                r.Cells[4].RowSpan = rowSpan;
            }
        }
    }
}